<?
require("../../common/init.php");
require(ROOT."common/image_zoom.php");
require("author.php");
require("upfile.php");

$gee=$product_variation; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象 gee=role object
$cate=$product; // cate = category, 分类

$cateid=isset($_GET["cid"])?clean($_GET["cid"]):0;
$url='product_var';
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$id=isset($_GET["id"])?clean($_GET["id"]):0;
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];

// 产品选项 options
$tem=$product_option->get_rows(32,'is_enabled','1','seq_no');
$arr_option=array();
foreach($tem as $val) $arr_option[$val['id']]=$val['name_'.$lang];

$cate_name=$cate->get_name($cateid,$lang);
$title=$cate_name." - ".ROOT_VARIATION;

// sql action / sql 动作
switch ($action){
	case "update":
		sql_update();
		break;
	case "add":
		sql_insert();
		break;
}

if($id==0){
	$order='id';
	$where=array();
	$where[]=array('name'=>'product_id','val'=>$cateid);
	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where,$order);
}
else $info=$gee->get_row($id);

function add()
{
	global $h, $url;
	$row = array();
	echo $h->form($url.'.php', 1, '');
	show_box($row, true, true);
	echo $h->hidden('action', 'add');
	echo $h->former();
}
function update($info)
{
	global $gee, $id, $h, $url;
	if($id==0)
	{
		echo "Invali argument!!!";
		exit;
	}
	echo $h->form($url.'.php', 1, '');
  	show_box($info, true);
	echo $h->hidden('action', 'update');
	echo $h->former();
}
function delete($id)
{
	global $gee, $url, $cateid, $page;
	if($gee->delete($id))
	wheel($url.'.php?cid='.$cateid.'&page='.$page);
}
function top($uid)
{
	global $dave,$url, $pid, $page_num;
	if($dave->onoff($uid,'isok')) wheel("".$url."-".$pid."-p".$page_num.".html");
}
function show_info($info)
{
	global $gee, $id;
	if($id==0)
	{
  	show_boxes($info);
	}
	else 
	{
		show_box($info, false);
	}
}
function sql_insert()
{
	global $gee, $url,$arrlang;
	$cateid=iso($_POST["cid"]);
	$arr=array(
		'seq_no'=>$_POST["seq_no"],
		'price'=>$_POST["price"],
		'stock'=>$_POST["stock"],
		'sku'=>$_POST["sku"],
		'product_id'=>$cateid,
	);
	if(isset($_POST['size'])) $arr['size']=$_POST['size'];
	if(isset($_POST['color'])) $arr['color']=$_POST['color'];
	if(isset($_POST['other1'])) $arr['other1']=$_POST['other1'];
	if(isset($_POST['other2'])) $arr['other2']=$_POST['other2'];
	if($_FILES["cpic"]["size"] > 0) {
		$pic=upfile($_FILES["cpic"]);
		$upload_folder='../../upload/';
		image_zoom($upload_folder.$pic, $upload_folder.$pic,PRODUCT_SMALL_PIC_WIDTH,PRODUCT_SMALL_PIC_HEIGHT,'#ffffff');
		$arr['pic']=$pic;
	}
	if($gee->insert($arr) )
	wheel($url.'.php?cid='.$cateid);
}
function sql_update()
{
	global $gee, $url,$arrlang;
	$delete_old_pic=0;
	$id=iso($_POST["id"]);
	$cateid=iso($_POST["cid"]);
	$arr=array(
		'seq_no'=>$_POST["seq_no"],
		'price'=>$_POST["price"],
		'stock'=>$_POST["stock"],
		'sku'=>$_POST["sku"],
		'product_id'=>$cateid,
	);
	if(isset($_POST['size'])) $arr['size']=$_POST['size'];
	if(isset($_POST['color'])) $arr['color']=$_POST['color'];
	if(isset($_POST['other1'])) $arr['other1']=$_POST['other1'];
	if(isset($_POST['other2'])) $arr['other2']=$_POST['other2'];
	if($_FILES["cpic"]["size"]>0) { // 更新图片
		$info=$gee->get_row($id);
		$upload_folder='../../upload/';
		$old_pic=$upload_folder.$info['pic'];
		$delete_old_pic=1;

		$pic=upfile($_FILES["cpic"]);
		image_zoom($upload_folder.$pic, $upload_folder.$pic,PRODUCT_SMALL_PIC_WIDTH,PRODUCT_SMALL_PIC_HEIGHT,'#ffffff');
		$arr['pic']=$pic;
	}
	if($gee->update($arr, 'id', $id )){
		if($delete_old_pic){
			@unlink($old_pic);
		}
	}
	wheel($url.'.php?cid='.$cateid);
}

// view 加载视图
require ROOTVIEW.$url.'.php';
